package data;

import java.util.TreeSet;



/**
 * describes a movie record
 * @author the secret 4 and the dog
 *
 */
public class MovieTuple {
	private String _name;
	private String _category;
	private String _director;
	private int _raters;
	private double _rating;
	private int _year;
	private int _length;
	private String _language;
	private TreeSet<RatingTuple> _ratings;
	private String _picture;

	/**
	 * creates a new instance of MovieTuple
	 * @param name
	 * @param category
	 * @param director
	 * @param raters
	 * @param year
	 * @param rating
	 * @param length
	 * @param lang
	 * @param picture
	 */
	public  MovieTuple(String name, String category, String director, String lang ,String raters,String rating, String year,  String length, String picture) {
		_name = name;
		_category = category;
		_director = director;
		_raters = Integer.parseInt(raters);
		_rating = Double.parseDouble(rating);
		_language = lang;
		_year = Integer.parseInt(year);
		_length = Integer.parseInt(length);
		//_ratings = new TreeSet<RatingTuple>(new CompartorUser());
		_ratings=null;
		//_picture = picture;
		_picture = "documentation/moviePictures/"+name+".jpg";
	}

	public void addExistingRating(RatingTuple rate){
		if (_ratings!=null)
			_ratings.add(rate);		
	}
	public void addRating(RatingTuple ratingTuple) {		
		if (_ratings!=null)
			_ratings.add(ratingTuple);	
		this._rating = (this._raters*this._rating + ratingTuple.get_rating())/(this._raters+1);
		_raters++;
	}


	public boolean equals( Object other){
		boolean ans =true;

		if (! ((MovieTuple )other)._name.equals(this._name)){
			ans=false;
		}
		if (!(((MovieTuple )other)._year==this._year)){
			ans=false;
		}


		return ans;
	}



	public String getKey(){
		return _name;
	}
	public String get_name() {
		return _name;
	}

	public String get_category() {
		return _category;
	}

	public String get_director() {
		return _director;
	}

	public int get_raters() {
		return _raters;
	}

	public double get_rating() {
		return _rating;
	}

	public int get_year() {
		return _year;
	}

	public int get_length() {
		return _length;
	}

	public String get_language() {
		return _language;
	}


	public TreeSet<RatingTuple> getRatings() {
		if (_ratings==null)
			_ratings=SystemData.getInstance().getRatingsByMovie(_name);
		return _ratings;
	}

	public String get_picture() {
		return _picture;
	}

	public void deleteRating(RatingTuple rating) {
		reduceMovieRating(rating.get_rating());
		_raters = _raters-1;
		if (_ratings!=null){
			_ratings.remove(rating);
		}
		

	}

	public TreeSet<RatingTuple> getCopyOfRatings() {					
		if (_ratings==null)
			getRatings();
		return (new TreeSet<RatingTuple>(_ratings));
	}

	//this function is called when a user adds a rating
	private void increaseMovieRating(double userRating){
		this._rating = (this._raters*this._rating + userRating)/(this._raters+1);
		
	}
	//this function is called when updating the movie rating due to user deleting his recommandation
	private void reduceMovieRating (double userRating){
		if (this._raters==1){
			this._rating=0;
		}
		else{
			this._rating = (this._raters*this._rating - userRating)/(this._raters-1);
		}
		
	}
	
	public void updateMovieRating(double oldRating, double newRating) {
		reduceMovieRating(oldRating);
		increaseMovieRating(newRating);
	}

	

}
